



//<Sensor_data_send>

#include <stdio.h>
#include <math.h>

char post[]=
   {
    "POST /v1.1/device/346055/sensor/aaaaaa/datapoints HTTP/1.1\r\n"
    "Host: api.yeelink.net\r\n"
    "Accept: */*\r\n"
    "U-ApiKey: 953caaa0d572086435e63e873bc650ca\r\n"
    "Content-Length: dt\r\n"
    "Content-Type: application/x-www-form-urlencoded\r\n"
    "Connection: close\r\n"
    "\r\n"
    "{\"value\":xxxxxx}\r\n"
   };

char post_reserve[]=
   {
    "POST /v1.1/device/346055/sensor/aaaaaa/datapoints HTTP/1.1\r\n"
    "Host: api.yeelink.net\r\n"
    "Accept: */*\r\n"
    "U-ApiKey: 953caaa0d572086435e63e873bc650ca\r\n"
    "Content-Length: dt\r\n"
    "Content-Type: application/x-www-form-urlencoded\r\n"
    "Connection: close\r\n"
    "\r\n"
    "{\"value\":xxxxxx}\r\n"
   };

char temperature_sensor_id[]={"385654"};
char humidity_sensor_id[]={"385659"};
char press_sensor_id[]={"385656"};
char lux_sensor_id[]={"385655"};
char direction_sensor_id[]={"385657"};
char wind_sensor_id[]={"385658"};

int i;
int len;
int times;
int temp;
////////////////////////////////////////////////////////
  void setNconnect()
    {
      int i;
      Serial2.println("ati");
        delay(1000);
      Serial2.println("ati");
        delay(1000);
      Serial2.println("ati");
        delay(1500);
        Serial2.println("AT+CGCLASS=\"B\"");
          delay(500);
          Serial2.println("AT+CGDCONT=1,\"IP\",\"CMNET\"");
            delay(500);
             Serial2.println("AT+CGATT=1");
              delay(500);
              Serial2.println("AT+CLPORT=\"TCP\",\"2000\"");
                delay(500);
                 Serial2.println("AT+CIPSTART=\"TCP\",\"42.96.164.52\",\"80\"");
                  delay(500);

      for(i=0;i<=9;i++);
        {
          delay(1000);
        }
      Serial.print("AT+CIPSEND");
      delay(500);
    }


  void closeconnecting()
    {
      Serial2.println("AT+CIPCLOSE=1");
        delay(500);
      Serial2.println("AT+CIPSHUT");
        delay(500);
    }

////////////////////////////////////////////////////////
void Sent_temperature_data()
{  setNconnect();
 len=strlen(post);
 for(times=0;times<len;times++)
 {
    if(post[times]=='a'&&post[times+1]=='a'&&post[times+2]=='a')
     {
        post[times]=temperature_sensor_id[0];
        post[times+1]=temperature_sensor_id[1];
        post[times+2]=temperature_sensor_id[2];
        post[times+3]=temperature_sensor_id[3];
        post[times+4]=temperature_sensor_id[4];
        post[times+5]=temperature_sensor_id[5];
        break;
     }
 }
 temp=0;
 len=strlen(post);
 temp=Serial3.print(temperature,1);
  for(times=0;times<len;times++)
 {
     if(post[times]=='d'&&post[times+1]=='t')
   {
      post[times]=(temp+10)/10+0x30;
      post[times+1]=(temp+10)%10+0x30;
      break;
   }
 }

 for(times=0;times<len;times++)
   {
     if(post[times]=='x'&&post[times+1]=='x'&&post[times+2]=='x')
   {
            Serial2.print(temperature,1);
            times=times+6;
    }
      Serial2.print(post[times]);
   }
  Serial2.write(26);//send order
  delay(5000);
  // closeconnecting();
 for(times=0;times<len;times++)
{
   post[times]=post_reserve[times];
}
}
////////2
void Sent_humidity_data() //
{  setNconnect();
 len=strlen(post);
 for(times=0;times<len;times++)
 {
    if(post[times]=='a'&&post[times+1]=='a'&&post[times+2]=='a')
     {
        post[times] = humidity_sensor_id[0];
        post[times+1]=humidity_sensor_id[1];
        post[times+2]=humidity_sensor_id[2];
        post[times+3]=humidity_sensor_id[3];
        post[times+4]=humidity_sensor_id[4];
        post[times+5]=humidity_sensor_id[5];
        break;
     }
 }
 temp=0;
 len=strlen(post);
 temp=Serial3.print(humidity);
  for(times=0;times<len;times++)
 {
     if(post[times]=='d'&&post[times+1]=='t')
   {
            post[times]=(temp+10)/10+0x30;
      post[times+1]=(temp+10)%10+0x30;
      break;
   }
 }

 for(times=0;times<len;times++)
   {
     if(post[times]=='x'&&post[times+1]=='x'&&post[times+2]=='x')
   {
            Serial2.print(humidity);
            times=times+6;
    }
      Serial2.print(post[times]);
   }
  Serial2.write(26);//send order
  delay(5000);
  //  closeconnecting();
 for(times=0;times<len;times++)
{
   post[times]=post_reserve[times];
}
}


//////

void Sent_press_data()
{  setNconnect();
 len=strlen(post);
 for(times=0;times<len;times++)
 {
    if(post[times]=='a'&&post[times+1]=='a'&&post[times+2]=='a')
     {
        post[times] = press_sensor_id[0];
        post[times+1]=press_sensor_id[1];
        post[times+2]=press_sensor_id[2];
        post[times+3]=press_sensor_id[3];
        post[times+4]=press_sensor_id[4];
        post[times+5]=press_sensor_id[5];
        break;
     }
 }
 temp=0;
 len=strlen(post);
 temp=Serial3.print(pressure,1);
  for(times=0;times<len;times++)
 {
     if(post[times]=='d'&&post[times+1]=='t')
          {
            post[times]=(temp+10)/10+0x30;
            post[times+1]=(temp+10)%10+0x30;
            break;
          }
 }

 for(times=0;times<len;times++)//填改传感器数值，发送报文
   {
     if(post[times]=='x'&&post[times+1]=='x'&&post[times+2]=='x')
   {
            Serial2.print(pressure,1);
            times=times+6;
    }
      Serial2.print(post[times]);
   }
  Serial2.write(26);//send order
  delay(5000);
  //  closeconnecting();
 for(times=0;times<len;times++)
{
   post[times]=post_reserve[times];
}
}
void Sent_wind_speed_data() //
{  setNconnect();
 len=strlen(post);
 for(times=0;times<len;times++)
 {
    if(post[times]=='a'&&post[times+1]=='a'&&post[times+2]=='a')
     {
        post[times] = wind_sensor_id[0];
        post[times+1]=wind_sensor_id[1];
        post[times+2]=wind_sensor_id[2];
        post[times+3]=wind_sensor_id[3];
        post[times+4]=wind_sensor_id[4];
        post[times+5]=wind_sensor_id[5];
        break;
     }
 }
 temp=0;
 len=strlen(post);
 temp=Serial3.print(wind_speed,1);
  for(times=0;times<len;times++)
 {
     if(post[times]=='d'&&post[times+1]=='t')
          {
            post[times]=(temp+10)/10+0x30;
            post[times+1]=(temp+10)%10+0x30;
            break;
          }
 }

 for(times=0;times<len;times++)
   {
     if(post[times]=='x'&&post[times+1]=='x'&&post[times+2]=='x')
   {
            Serial2.print(wind_speed,1);
            times=times+6;
    }
      Serial2.print(post[times]);
   }
  Serial2.write(26);//send order
  delay(5000);
  //  closeconnecting();
 for(times=0;times<len;times++)
{
   post[times]=post_reserve[times];
}
}
///////////////////////////////
void Sent_wind_dir_data()
{  setNconnect();
 len=strlen(post);
 for(times=0;times<len;times++)
 {
    if(post[times]=='a'&&post[times+1]=='a'&&post[times+2]=='a')
     {
        post[times] = direction_sensor_id[0];
        post[times+1]=direction_sensor_id[1];
        post[times+2]=direction_sensor_id[2];
        post[times+3]=direction_sensor_id[3];
        post[times+4]=direction_sensor_id[4];
        post[times+5]=direction_sensor_id[5];
        break;
     }
 }
 temp=0;
 len=strlen(post);
 temp=Serial3.print(dir);
  for(times=0;times<len;times++)
 {
     if(post[times]=='d'&&post[times+1]=='t')
   {
      post[times]=(temp+10)/10+0x30;
      post[times+1]=(temp+10)%10+0x30;
      break;
   }
 }

 for(times=0;times<len;times++)
   {
     if(post[times]=='x'&&post[times+1]=='x'&&post[times+2]=='x')
   {
            Serial2.print(dir);
            times=times+6;
    }
      Serial2.print(post[times]);
   }
  Serial2.write(26);//send order
  delay(5000);
  //  closeconnecting();
 for(times=0;times<len;times++)
{
   post[times]=post_reserve[times];
}
}

////////////////
void Sent_lux_data()
{  setNconnect();
 len=strlen(post);
 for(times=0;times<len;times++)
 {
    if(post[times]=='a'&&post[times+1]=='a'&&post[times+2]=='a')
     {
        post[times] = lux_sensor_id[0];
        post[times+1]=lux_sensor_id[1];
        post[times+2]=lux_sensor_id[2];
        post[times+3]=lux_sensor_id[3];
        post[times+4]=lux_sensor_id[4];
        post[times+5]=lux_sensor_id[5];
        break;
     }
 }
 temp=0;
 len=strlen(post);
 temp=Serial3.print(lux);
  for(times=0;times<len;times++)
    {
     if(post[times]=='d'&&post[times+1]=='t')
     {
      post[times]=(temp+10)/10+0x30;
      post[times+1]=(temp+10)%10+0x30;
      break;
    }
    }

 for(times=0;times<len;times++)
   {
        if(post[times]=='x'&&post[times+1]=='x'&&post[times+2]=='x')
          {
            Serial2.print(lux);        //fix
            times=times+6;
          }
      Serial2.print(post[times]);
   }
  Serial2.write(26);//send order
  delay(5000);
  //  closeconnecting();
 for(times=0;times<len;times++)
  {
    post[times]=post_reserve[times];
  }
}
